/*
 * DBEntity.java
 *
 * Created on May 1, 2007, 3:23 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.plotnikov.pricemaker.utils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Vector;
import org.plotnikov.pricemaker.error.RootException;

/**
 *
 * @author kostya
 */
public class DBEntity implements Serializable{
    
    Vector<DataChunk> fields = new Vector();
//    Vector<String> names = new Vector();
    /** Creates a new instance of DBEntity */
    public DBEntity() {
    }
    
//    public DBEntity(Object o) {
//        QueryParser.parseObject(o, fields);
//    }
    
    public DBEntity(String id, String name) {
        setField("id", id);
        setField("name", name);
    }
    
    public DBEntity(Object id, String name) {
        setField("id", id);
        setField("name", name);
    }
    
    public DBEntity(int id, String name) {
        setField("id", new BigDecimal(id));
        setField("name", name);
    }
    
//    public static DBEntity[] queryToArray(List l, DBEntity firstElement){
//        List<DBEntity> res = new ArrayList();
//        if(firstElement!=null)
//            res.add(firstElement);
//        for (Object elem : l) {
//            res.add(new DBEntity(elem));
//        }
//        return res.toArray(new DBEntity[0]);
//    }
//    public static DBEntity[] queryToArray(List l){
//        return queryToArray(l, null);
//    }
    
    public void setField(String name, Object value){
        if(value==null)
            return;
        DataChunk chunk = null;
        if(value instanceof Number)
            chunk = new DataChunk(name, new Long(((Number)value).longValue()));
        else
            chunk = new DataChunk(name, value);
        if(fields.contains(chunk))
            fields.elementAt(fields.indexOf(chunk)).setData(chunk.getData());
        else
            fields.addElement(chunk);
    }
    
    public Object getObject(String name){
        for (DataChunk elem : fields) {
            if(elem.getId().equals(name))
                return elem.getData();
        }
        return null;
    }
    
    public <T> T getObject(String name, Class<T> entityClass){
        for (DataChunk elem : fields) {
            if(elem.getId().equals(name))
                return (T)elem.getData();
        }
        return null;
    }
    
    public String getString(String name){
        Object o = getObject(name);
        if(o!=null)
            return o.toString();
        return "";
    }
    
    public BigDecimal getBigDecimal(String name){
        try{
            return new BigDecimal(getString(name));
        }catch (Throwable t){
//            t.printStackTrace();
            return new BigDecimal("0");
        }
    }
    
    public BigInteger getBigInteger(String name){
        try{
            return new BigInteger(getString(name));
        }catch (Throwable t){
//            t.printStackTrace();
            return new BigInteger("0");
        }
    }
    
    public int getInt(String name) throws RootException{
        if(getObject(name)!=null){
            if(getObject(name) instanceof Number)
                return ((Number)getObject(name)).intValue();
            try {
                return Integer.parseInt(getObject(name).toString());
            } catch (Throwable t) {
//                t.printStackTrace();
            }
        }
        throw new RootException("can't cast value: "+getObject(name)+" to Int");
    }

    public String toString2() {
        String s = "["+getClass().getName();
        for (DataChunk chunk : fields) {
            s +=","+chunk.getId()+"="+chunk.getData();
        }
        return s+"]";
    }

    @Override
    public String toString() {
        return getString("name");
    }
    
    
    public DataChunk getFieldAt(int index){
        return fields.elementAt(index);
    }
    
    public int getFieldCount(){
        return fields.size();
    }

    public Object getObject(int columnIndex) {
        return fields.elementAt(columnIndex).getData();
    }

    public String getName(int index){
        return fields.elementAt(index).getId();
    }
    
    private void copyFrom(Vector<DataChunk> t){
        fields =  (Vector<DataChunk>) t.clone();
    }
    
    public DBEntity makeClone(){
        DBEntity c = new DBEntity();
        c.copyFrom(fields);
        return c;
    }
    
    public Vector<String> compareWith(DBEntity e){
        Vector<String> res = new Vector();
        for (int i = 0; i < getFieldCount(); i++) {
            String name = getName(i);
            if(!getString(name).equals(e.getString(name)))
                res.add(getName(i)+": "+e.getString(name)+" на "+getString(name));
        }
        return res;
    }
}
